package com.lfd.netty;

import com.lfd.netty.netty.NettyClient;
import com.lfd.netty.netty.NettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NettyApplication implements ApplicationRunner {
    @Autowired
    private NettyServer nettyServer;
    @Autowired
    private NettyClient nettyClient;

    public static void main(String[] args) {
        SpringApplication.run(NettyApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    nettyServer.start();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
        // 确保服务端启动完成
        Thread.sleep(5000); // 等待1秒，确保服务端已经启动

        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    nettyClient.start();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
    }
}
